

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>fatghol.aggregate &mdash; FatGHoL v5.4 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '5.4',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="FatGHoL v5.4 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-23673826-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">FatGHoL v5.4 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for fatghol.aggregate</h1><div class="highlight"><pre>
<span class="c">#! /usr/bin/env python</span>
<span class="c">#</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">The `AggregateList` class.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="c">#</span>
<span class="c">#   Copyright (C) 2008-2012 Riccardo Murri &lt;riccardo.murri@gmail.com&gt;</span>
<span class="c">#   All rights reserved.</span>
<span class="c">#</span>
<span class="c">#   This program is free software: you can redistribute it and/or modify</span>
<span class="c">#   it under the terms of the GNU General Public License as published by</span>
<span class="c">#   the Free Software Foundation, either version 3 of the License, or</span>
<span class="c">#   (at your option) any later version.</span>
<span class="c">#</span>
<span class="c">#   This program is distributed in the hope that it will be useful,</span>
<span class="c">#   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c">#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c">#   GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c">#   You should have received a copy of the GNU General Public License</span>
<span class="c">#   along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c">#</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s">&#39;reStructuredText&#39;</span>

<span class="c">#import cython</span>

<span class="kn">import</span> <span class="nn">itertools</span>

<span class="c">#@cython.cclass</span>
<div class="viewcode-block" id="AggregateList"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList">[docs]</a><span class="k">class</span> <span class="nc">AggregateList</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Act on a set of lists as if they were concatenated together.</span>

<span class="sd">    The aggregate list acts as a collective: the length is the sum of</span>
<span class="sd">    all the components&#39; length, iteration over the composite list</span>
<span class="sd">    reveals all the components members in the order the components</span>
<span class="sd">    were added.  References to elements of the aggregate list are</span>
<span class="sd">    routed to the component lists, and iteration over the aggregate</span>
<span class="sd">    list returns the elements of the component lists (in the order the</span>
<span class="sd">    components were added to the aggregate)::</span>

<span class="sd">      &gt;&gt;&gt; a1 = [0,1,2]</span>
<span class="sd">      &gt;&gt;&gt; a2 = [3, 4]</span>
<span class="sd">      &gt;&gt;&gt; a = AggregateList(a1, a2)</span>
<span class="sd">      &gt;&gt;&gt; len(a)</span>
<span class="sd">      5</span>
<span class="sd">      &gt;&gt;&gt; a[1]</span>
<span class="sd">      1</span>
<span class="sd">      &gt;&gt;&gt; a[4]</span>
<span class="sd">      4</span>
<span class="sd">      &gt;&gt;&gt; a[4] = 77</span>
<span class="sd">      &gt;&gt;&gt; 77 in a</span>
<span class="sd">      True</span>
<span class="sd">      &gt;&gt;&gt; list(a)</span>
<span class="sd">      [0, 1, 2, 3, 77]</span>

<span class="sd">    Operations are routed to the appropriate component list: e.g.,</span>
<span class="sd">    `append` appends to the last list in the group.</span>

<span class="sd">      &gt;&gt;&gt; a.append(5)</span>
<span class="sd">      &gt;&gt;&gt; a2</span>
<span class="sd">      [3, 77, 5]</span>

<span class="sd">    *Note:* Currently not implemented:</span>
<span class="sd">      - remove()</span>
<span class="sd">      - pop()</span>
<span class="sd">      - count()</span>
<span class="sd">      - sort()</span>
<span class="sd">      - reverse()</span>
<span class="sd">      - All slicing operations.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">seqs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor, taking initial list of</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__components</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">seqs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">seqs</span> <span class="p">]</span>

    <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">component</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">component</span><span class="o">.</span><span class="n">__contains__</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">True</span>

    <span class="c">#@cython.locals(i=cython.int, l=cython.int, n=cython.int)</span>
    <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">component</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">):</span>
            <span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">component</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="n">l</span><span class="p">:</span>
                <span class="n">i</span> <span class="o">-=</span> <span class="n">l</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">component</span><span class="o">.</span><span class="n">__delitem__</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">-=</span> <span class="mi">1</span>
        <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">&quot;AggregateList.__delitem__(): list assignment out of range&quot;</span><span class="p">)</span>

    <span class="c">#@cython.locals(i=cython.int, l=cython.int)</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">component</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">:</span>
            <span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">component</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="n">l</span><span class="p">:</span>
                <span class="n">i</span> <span class="o">-=</span> <span class="n">l</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">component</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">&quot;AggregateList.__getitem__(): list assignment out of range&quot;</span><span class="p">)</span>

    <span class="c">#@cython.ccall</span>
    <span class="k">def</span> <span class="nf">iterblocks</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">)</span>

    <span class="c">#@cython.ccall</span>
    <span class="k">def</span> <span class="nf">itervalues</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">itervalues</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="p">)</span>

    <span class="c">#@cython.locals(i=cython.int, l=cython.int)</span>
    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">component</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">:</span>
            <span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">component</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="n">l</span><span class="p">:</span>
                <span class="n">i</span> <span class="o">-=</span> <span class="n">l</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">component</span><span class="o">.</span><span class="n">__setitem__</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
                <span class="k">return</span>
        <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">&quot;AggregateList.__setitem__(): list assignment out of range&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="AggregateList.aggregate"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList.aggregate">[docs]</a>    <span class="k">def</span> <span class="nf">aggregate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">seqs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Append each sequence in `seqs` to the aggregated list.</span>

<span class="sd">        Example::</span>

<span class="sd">          &gt;&gt;&gt; a = AggregateList([0,1,2])</span>
<span class="sd">          &gt;&gt;&gt; a.aggregate([3,4],[5,6])</span>
<span class="sd">          &gt;&gt;&gt; list(a)</span>
<span class="sd">          [0, 1, 2, 3, 4, 5, 6]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">seqs</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>

    <span class="c">#@cython.ccall</span></div>
<div class="viewcode-block" id="AggregateList.aggregate1"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList.aggregate1">[docs]</a>    <span class="k">def</span> <span class="nf">aggregate1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seq</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Append sequence `seq` to the aggregate list.</span>

<span class="sd">        Example::</span>

<span class="sd">          &gt;&gt;&gt; a = AggregateList([0,1,2])</span>
<span class="sd">          &gt;&gt;&gt; a.aggregate([3,4])</span>
<span class="sd">          &gt;&gt;&gt; a.aggregate([5,6])</span>
<span class="sd">          &gt;&gt;&gt; list(a)</span>
<span class="sd">          [0, 1, 2, 3, 4, 5, 6]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">seq</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">seq</span><span class="p">))</span>

    <span class="c">#@cython.cfunc</span></div>
<div class="viewcode-block" id="AggregateList.append"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList.append">[docs]</a>    <span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Append `item` to the last component.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
        
    <span class="c">#@cython.cfunc</span></div>
<div class="viewcode-block" id="AggregateList.extend"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList.extend">[docs]</a>    <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seq</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Extend the last component with `seq`.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">seq</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__lengths</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

    <span class="c">#@cython.cfunc(cython.int)</span>
    <span class="c">#@cython.locals(index=cython.int, l=cython.int)</span></div>
<div class="viewcode-block" id="AggregateList.index"><a class="viewcode-back" href="../../api.html#fatghol.aggregate.AggregateList.index">[docs]</a>    <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return index of the first element equal to `value`.&quot;&quot;&quot;</span>
        <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">component</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__components</span><span class="p">:</span>
            <span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">component</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">start</span> <span class="o">&gt;</span> <span class="n">l</span><span class="p">:</span>
                <span class="n">start</span> <span class="o">-=</span> <span class="n">l</span>
                <span class="n">index</span> <span class="o">+=</span> <span class="n">l</span>
                <span class="k">continue</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">index</span> <span class="o">+</span> <span class="n">component</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">start</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">continue</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;AggregateList.index(): x not in list&quot;</span><span class="p">)</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">FatGHoL v5.4 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2012, Riccardo Murri.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>